<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>课程分配</title>
    <link rel="stylesheet" href="../../component/pear/css/pear.css"/>
</head>

<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
            <legend>课程分配</legend>
        </fieldset>
        <br>
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">记录查询</label>
                <div class="layui-inline">
                    <input type="text" name="key" placeholder="输入课程名称/教师名称" class="layui-input">
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="layui-btn layui-btn-sm" lay-submit
                            lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                </div>
            </div>
        </form>
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <button class="layui-btn layui-btn-sm" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增课程分配
    </button>
</script>

<script type="text/html" id="user-bar">
    <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">
        <i class="layui-icon layui-icon-delete"></i>
    </button>
</script>

<script src="../../../static/layui/layui.js"></script>
<script src="../../component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'common'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let MODULE_PATH = "operate/";

        let cols = [
            [{
                type: 'numbers'
            },
                {
                    title: 'relationId',
                    field: 'relationId',
                    align: 'center',
                    hide:true
                },
                {
                    title: '课程名称',
                    field: 'courseName',
                    align: 'center'
                },
                {
                    title: '所属专业',
                    field: 'majorName',
                    align: 'center'
                },
                {
                    title: '所属学院',
                    field: 'collegeName',
                    align: 'center'
                },
                {
                    title: '主讲教师',
                    field: 'teacherName',
                    align: 'center'
                },
                {
                    title: '教师ID',
                    field: 'teacherId',
                    align: 'center'
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'center',
                    width: 130
                }
            ]
        ]


        let tableIns = table.render({
            elem: '#user-table',
            url: '/relation/getAllRelation',
            method: "post",
            initSort: {
                field: 'startTime' //排序字段，对应 cols 设定的各字段名
                , type: 'desc' //排序方式  asc: 升序、desc: 降序、null: 默认排序
            },
            page: {
                layout: ['count', 'prev', 'page', 'next', 'skip']
                , curr: 1
                , groups: 3
                , last: false
            },
            limit: 5,
            //limits:[5,10,20],
            cols: cols,
            id: 'user-table',
            skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新列表',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }],
            //一定要加这个参数代码，否则会由于layui自身的bug导致无法分页
            parseData: function (res) { //将原始数据解析成 table 组件所规定的数据，res为从url中get到的数据
                let result;
                // console.log(res.data[0].birthday);
                // res.data[0].birthday = dateFormat(res.data[0].birthday, 'Y-M-D')
                if (this.page.curr) {
                    result = res.data.slice(this.limit * (this.page.curr - 1), this.limit * this.page.curr);
                } else {
                    result = res.data.slice(0, this.limit);
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": result //解析数据列表
                };
            }
        });

        form.on('submit(user-query)', function (data) {
            tableIns.reload({
                url: '/relation/getRelationByKey',
                method: 'post',
                page: {
                    layout: ['count', 'prev', 'page', 'next', 'skip']
                    , curr: 1
                    , groups: 3
                    , last: false
                }
                , where: data.field
            })
            return false;
        });

        table.on('tool(user-table)', function (obj) {
            if (obj.event === 'del') {
                window.del(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(user-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            }
        });
        window.del = function (obj) {
            //console.log(obj.data['userId']);
            layer.confirm('确定要删除这条课程关联吗', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/relation/delRelation",
                    data: {
                        relationId: obj.data['relationId']
                    },
                    dataType: 'json',
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                tableIns.reload({
                                    url: "/relation/getAllRelation",
                                    page: {
                                        layout: ['count', 'prev', 'page', 'next', 'skip']
                                        , curr: $(".layui-laypage-em").next().html()
                                        , groups: 3
                                        , last: false
                                    },
                                    method: "post"
                                });
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.add = function () {
            layer.open({
                type: 2,
                title: '新增课程分配',
                shade: 0.1,
                shadeClose:true,
                area:['50%', '60%'],
                content: MODULE_PATH + 'addRelation.html'
            });
        }

        window.refresh = function (param) {
            table.reload('user-table', {
                url: "/relation/getAllRelation",
                method: "post"
            });
        }
    })
</script>
</body>

</html>